﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Lswweb.Domain;

namespace Lswweb.Controls
{
    public class Language : SkinnedWebControl
    {
        private IButtonControl btnCN;
        private IButtonControl btnEN;

        protected override void InitializeSkin(System.Web.UI.Control skin)
        {
            btnCN = (IButtonControl)skin.FindControl("btnCN");
            btnEN = (IButtonControl)skin.FindControl("btnEN");

            btnCN.Click += new EventHandler(btnCN_Click);
            btnEN.Click += new EventHandler(btnTW_Click);
        }

        void btnTW_Click(object sender, EventArgs e)
        {
            SiteContext.Current.Language = "en";
            Redirect();
        }

        void btnCN_Click(object sender, EventArgs e)
        {
            SiteContext.Current.Language = "zh-cn";
            Redirect();
        }

        private void Redirect()
        {
            //因为中英文可能url参数不同，所以不能转到当前页面
            string url = SiteUrls.Urls.Home;
            if (SiteContext.Current.User != null
                && SiteContext.Current.User.IsAdmin
                && SiteContext.Current.MapNode != null
                && SiteContext.Current.MapNode.Type== MapType.AdminMenu)
            {
                url = SiteUrls.Urls.AdminHome;
            }

            SiteContext.Current.Context.Response.Redirect(url, true);
        }

        protected override string DefaultSkinFilename
        {
            get { return "Language.ascx"; }
        }
    }
}
